#ifndef LECTURER_LIB_H_
#define LECTURER_LIB_H_

#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <sstream>

#include "Lecturer.h"

using namespace std;

void Add_lecturer(string s, Lecturer l[], int &numberLecturer){
    string temp, a_fname, a_dob, a_addr, a_field, a_university;
    int a_id;
    short a_lec_year;

    int mark = s.find(":");
    a_id = atoi(s.substr(0,mark).c_str());


    temp = s.substr(mark+1);
    mark = temp.find(":");
    a_fname = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_dob = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_addr = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_field = temp.substr(0,mark);

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_lec_year = atoi(temp.substr(0,mark).c_str());

    temp = temp.substr(mark+1);
    mark = temp.find(":");
    a_university = temp.substr(0,mark);

    Lecturer lec = Lecturer(a_id, a_fname, a_dob, a_addr, a_field, a_lec_year, a_university);
    l[numberLecturer] = lec;
    numberLecturer++;
}

void Read_lecturer(string _file, Lecturer l[], int &numberLecturer){
    ifstream fin;
    fin.open(_file.c_str());
    if(!fin.is_open()){
        cout<<"Cannot open file.\n";
        exit(1);
    }else{
        string s = "";
        while (getline(fin, s)){
            Add_lecturer(s,l,numberLecturer);
        }
        fin.close();
    }
}

Lecturer Get_lecturer_by_id(int key, Lecturer l[], int numberLecturer){
    int i = 0;
    Lecturer result(0, "", "", "", "", 0, "");
    for (i = 0; i < numberLecturer; i++){
        if (l[i].Get_id() == key){
            return l[i];
        }
    }

    return result;
}

void Get_lectures_by_name(string key, Lecturer l[], int numberLecturer, Lecturer result[], int &numberLecturerResult){
    int i = 0;
    for (i = 0; i < numberLecturer; i++){
        if (l[i].Get_fname().find(key) != std::string::npos){
            result[numberLecturerResult] = l[i];
            numberLecturerResult++;
        }
    }
}

#endif // LECTURER_LIB_H_
